package com.msh.starter.captcha.bean;

import com.msh.frame.interfaces.IQuestionAnswerGen;
import com.msh.starter.captcha.consts.CaptchaArrayConst;
import com.msh.starter.captcha.enums.CaptchaArithmeticEnum;
import com.msh.starter.captcha.util.CaptchaUtil;

public class ArithmeticCaptchaGen implements IQuestionAnswerGen {
  private static final int length = 5;
  /**
   * 验证码类型
   */
  protected CaptchaArithmeticEnum type;


  public ArithmeticCaptchaGen() {
    this(CaptchaArithmeticEnum.ARITHMETIC);
  }

  public ArithmeticCaptchaGen(CaptchaArithmeticEnum type) {
    this.type = type;
  }

  @Override
  public QuestionAnswer gen() {
    String question = question();
    String answer = answer(question);
    return new QuestionAnswer(question, answer);
  }

  private String question(){
    char[] codes = new char[length];
    for(int i=0;i<length;i++){
      switch (type){
        case ARITHMETIC_ZH:
          codes[i] = arithmeticExp(i,true);
          break;
        default: codes[i] = arithmeticExp(i,false);
      }
    }
    return String.valueOf(codes);
  }

  private String answer(String question){
    if(type.equals(CaptchaArithmeticEnum.ARITHMETIC)|| type.equals(CaptchaArithmeticEnum.ARITHMETIC_ZH)){
      return arithmeticResult(question.toCharArray());
    }
    return question;
  }


  /**
   * 获取运算表达式的计算值
   * @param codes     运算表达式字符
   * @return          运算结果
   */
  private static String arithmeticResult(char[] codes){
    char operator = codes[1];
    int num_1 = analysisChar(codes[0]);
    int num_2 = analysisChar(codes[2]);
    if(operator=='+' || operator == '\u52a0'){
      return String.valueOf((num_1+num_2));
    }else if(operator == '-' || operator == '\u51cf'){
      return String.valueOf((num_1 - num_2));
    }else if(operator == '*' || operator == 'x' || operator == 'X' || operator == '\u4e58'){
      return String.valueOf((num_1*num_2));
    }else{
      return "0";
    }
  }


  /**
   * 解析运算表达式字符
   * @param character     表达式字符
   * @return              罗马数字字符
   */
  private static int analysisChar(char character){
    if(character == '0' || character == '\u96f6'){
      return 0;
    }else if(character == '1' || character == '\u58f9' || character == '\u4e00'){
      return 1;
    }else if(character == '2' || character == '\u8d30' || character == '\u4e8c'){
      return 2;
    }else if(character == '3' || character == '\u53c1' || character == '\u4e09'){
      return 3;
    }else if(character == '4' || character == '\u8086' || character == '\u56db'){
      return 4;
    }else if(character == '5' || character == '\u4f0d' || character == '\u4e94'){
      return 5;
    }else if(character == '6' || character == '\u9646' || character == '\u516d'){
      return 6;
    }else if(character == '7' || character == '\u67d2' || character == '\u4e03'){
      return 7;
    }else if(character == '8' || character == '\u634c' || character == '\u516b'){
      return 8;
    }else if(character == '9' || character == '\u7396' || character == '\u4e5d'){
      return 9;
    }else{
      return 0;
    }
  }

  /**
   * 生成十位数以内的运算表达式的字符（不包含十位数）
   * @param i             字符位置
   * @param zh_cn         是否为中文简体形式
   * @return              下标为i的字符
   */
  private static char arithmeticExp(int i,boolean zh_cn){
    if(zh_cn){
      switch (i){
        case 0:
        case 2:
          return CaptchaArrayConst.NUM_ZH_CN[CaptchaUtil.nextInt(CaptchaArrayConst.NUM_ZH_CN.length)];
        case 1:
          return CaptchaArrayConst.OPERATOR_ZH[CaptchaUtil.nextInt(CaptchaArrayConst.OPERATOR_ZH.length)];
        case 3:
          return '=';
        default: return '?';
      }
    }else{
      switch (i){
        case 0:
        case 2:
          return CaptchaArrayConst.NUM_ARABIC[CaptchaUtil.nextInt(CaptchaArrayConst.NUM_ARABIC.length)];
        case 1:
          return CaptchaArrayConst.OPERATOR_EN[CaptchaUtil.nextInt(CaptchaArrayConst.OPERATOR_EN.length)];
        case 3:
          return '=';
        default: return '?';
      }
    }
  }
}
